<template>
  <header class="head_slot-wrapper">
    <!-- 顶部容器 -->
    <div class="top_nav_slot-wrapper">
      <!-- 这俩东西是全屏的 -->
      <div :style="comBackground" class="nav-bg" />
      <!-- 这个是中间限定的容器 -->
      <div class="top_nav-group">
        <!-- 左边插槽 (站点功能) -->
        <div class="top_nav-slot">
          <slot name="left" />
        </div>
        <!-- 右边插槽 (用户功能) -->
        <div class="top_nav-slot">
          <slot name="right" />
        </div>
      </div>
    </div>
    <!-- 中间插槽 (logo) -->
    <div :style="comBackground" class="banner-bg">
      <div class="banner-group">
        <slot name="middle" />
      </div>
    </div>
    <!-- 顶部插槽 (分类菜单) -->
    <div class="menu-group" ref="menu">
      <div :class="['fixed-wrapper',{fixed:menuFixed}]">
        <slot :fixed="menuFixed" name="bottom" />
      </div>
    </div>
  </header>
</template>

<script>
import { getElementTop } from '@/utils';

export default {
    props: {
        backgroundImage: {
            type: String,
            default: 'https://resource.miva.work/articleArchiveCover/159177871902781826a53ab16db1277ffd70fd41296a4.jpg',
        },
    },
    data() {
        return {
            scrollY: 0,
            menuY: 0,
        };
    },
    computed: {
        menuFixed() {
            return this.scrollY >= this.menuY;
        },
        gonggaoShow() {
            return this.$store.state.app.gonggaoShow;
        },
        cutBackgroundImage() {
            return `${this.backgroundImage}?x-oss-process=image/resize,m_fill,w_1920,h_180`;
        },
        comBackground() {
            return `background-image: url(${this.cutBackgroundImage});`;
        },
    },
    mounted() {
        this.menuY = getElementTop(this.$refs.menu);
        window.addEventListener('scroll', this.scrollHandler, { passive: true });
    },
    destroyed() {
        window.removeEventListener('scroll', this.scrollHandler, { passive: true });
    },
    methods: {
        scrollHandler() {
            this.scrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        },
    },
};
</script>

<style lang="less" scoped>
// 公告:3, 顶部导航:2,页头背景:1, 底部导航:0,
.head_slot-wrapper {
  min-width: @baseWidth;
  position: relative;
  z-index: @base_z-index;
  border-bottom: 1px solid #ddd;
  background: #fff;
  user-select: none;

  .top_nav_slot-wrapper {
    height: @headNavHeight;
    position: relative;
    z-index: 2;

    .i-nav-base() {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }

    &::before {
      .i-nav-base();
      content: "";
      display: block;
      background-color: hsla(0, 0%, 100%, 0.4);
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
      z-index: 1;
    }

    .nav-bg {
      .i-nav-base();
      background-size: 1920px, 180px;
      background-position: center -10px;
      background-repeat: no-repeat;
      filter: blur(4px);
    }

    .top_nav-group {
      width: @baseWidth;
      margin: 0 auto;
      display: flex;
      justify-content: space-between;

      .top_nav-slot {
        position: relative;
      }
    }
  }

  .banner-bg {
    height: @headMiddleHeight;
    margin-top: -@headNavHeight; // 实属无奈...
    z-index: 1;
    background: #eee;
    background-size: 1920px, 180px;
    background-position: center -10px;
    background-repeat: no-repeat;

    .banner-group {
      width: @baseWidth;
      height: 100%;
      margin: 0 auto;
      position: relative;
    }
  }

  .menu-group {
    width: @baseWidth;
    height: @headMenuHeight;
    margin: 0 auto;
    position: relative;
    background-color: #fff;

    > .fixed-wrapper {
      width: @baseWidth;
      height: @headMenuHeight;
      margin: 0 auto;
      position: relative;
      background-color: #fff;
    }

    .fixed {
      position: fixed;
      top: 0;
      left: 50%;
      transform: translateX(-50%);
      opacity: 0.95;
      z-index: 99;
    }

    .fixed::after {
      content: "";
      display: flex;
      width: 100vw;
      height: 100%;
      position: fixed;
      top: 0;
      left: 50%;
      z-index: -1;
      transform: translateX(-50%);
      border-bottom: 1px solid #ddd;
      background-color: inherit;
    }
  }
}
</style>
